from typing import List


class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort(key=lambda x: x[0])
        res=[intervals[0]]
        for i in range(1,len(intervals)):
            pair=intervals[i]
            last=res[-1]
            if last[0]==pair[0]:
                last[1]=max(last[1],pair[1])
            elif last[1]>=pair[0]:
                last[1]=max(last[1],pair[1])
            else:
                res.append(pair)

        return res


if __name__=='__main__':
    solution = Solution()
    intervals=[[1,4],[4,5]]
    res=solution.merge(intervals)
    print(res)